package com.xlh.virtualization.constant;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * 容器状态枚举
 *
 * @author cheer
 */
public enum ContainerStatusEnum {

    RUNNING(1, "运行"),
    EXITED(2, "停止"),
    STOPPING(3, "停止中");

    private Integer code;
    private String message;

    ContainerStatusEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 把前端的容器状态转成数据库的容器状态
     */
    public static List<Integer> toDatabase(Integer code) {
        ContainerStatusEnum containerStatusEnum = forCode(code);
        if (containerStatusEnum == null) {
            return null;
        }
        if (containerStatusEnum == EXITED) {
            return Lists.newArrayList(EXITED.code, STOPPING.code);
        }
        return Lists.newArrayList(containerStatusEnum.code);
    }

    /**
     * 把数据库的容器状态转成前端的容器状态
     */
    public static Integer toFrontend(Integer code) {
        ContainerStatusEnum containerStatusEnum = forCode(code);
        if (containerStatusEnum == null) {
            return null;
        }
        if (containerStatusEnum == STOPPING) {
            return EXITED.code;
        }
        return containerStatusEnum.code;
    }

    public static ContainerStatusEnum forCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (ContainerStatusEnum containerStatusEnum : ContainerStatusEnum.values()) {
            if (containerStatusEnum.code.equals(code)) {
                return containerStatusEnum;
            }
        }
        return null;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

